﻿using imaginecup.objectManager;
using imaginecup.util;
using Imaginecup.gameState.malaria.componentInit;
using Microsoft.Xna.Framework;
using Imaginecup.gameState.malaria.component.children;
using Imaginecup.util;
using Imaginecup.factory;
using Imaginecup.gameState.malaria.component.factory;
using System.Collections.Generic;

namespace Imaginecup.gameState.malaria.manager
{
    public class ChildInfo
    {
        public double m_AppearTime;
        public double m_Velocity;

        public ChildInfo( double appearTime , double velocity)
        {
            m_AppearTime = appearTime;
            m_Velocity = velocity;
        }
    }
    public class ChildGenerator
    {
        private ObjectManager m_ObjectManager;

        private List<ChildInfo> m_ChildInfoList = new List<ChildInfo>();
        private double m_CurrentTime;

        public ChildGenerator()
        {
            m_CurrentTime = 0;
        }

        public void AddChildInfo( ChildInfo info )
        {
            m_ChildInfoList.Add(info);
        }
      
        public void SetObjectManager(ObjectManager objMan)
        {
            ObjectFactory factory = ObjectFactory.GetObjectFactory();
            m_ObjectManager = objMan;
            factory.SetObjectMan(m_ObjectManager);
        }

        public void Update(GameTime gameTime)
        {
            m_CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;
            ChildInfo info;
            for( int i = m_ChildInfoList.Count - 1 ; i >= 0 ; i-- )
            {
                info = m_ChildInfoList[i];

                if( m_CurrentTime > info.m_AppearTime )
                {
                    GeneratorChild(info.m_Velocity);
                    m_ChildInfoList.RemoveAt(i);
                }
            }
        }

        private void GeneratorChild(double p)
        {
            ObjectFactory factory = ObjectFactory.GetObjectFactory();


            int CharSizeX = 50;
            int CharSizeY = 50;

            EntityInfo charInfo = new EntityInfo();

            charInfo.mSize = new Vector2(CharSizeX, CharSizeY);
            charInfo.mPosition = new Vector2(800, 320);

            CmpChildren entity = (CmpChildren)factory.GetObject(MALARIA_COMPONENT_ID.CHAR);
            entity.SetEntityInfo(charInfo);
            entity.SetVelocity(-p);
        }


        public int GetChildNum()
        {
            return m_ChildInfoList.Count;
        }
    }
}
